if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");

var time = random(2500, 3500)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result; 
}

function task() {
  var 任务 = id("ivTask").findOne(1000);
  if(任务){
    任务.click();
    var index = 1
    while (index < 3) {
      toastLog('第' + index + '次去完成任务')
      sleep(time)
      var 去完成 = textEndsWith('/4)').findOne(5000)
      if (去完成) {
        var num = parseFloat(去完成.text().slice(4, 5))
        if (num < 4) {
          toastLog('找到了' + (4 - num) + '个广告')
          sleep(time)
          for (var i = 0; i < 4 - num; i++) {
            var 去完成 = textEndsWith('/4)').findOne(5000)
            toastLog('第' + (i + 1) + '次观看广告')
            var finalActivity = currentActivity();
            sleep(time)
            点击控件(去完成)
            sleep(10000)
            toastLog('当前是广告界面')
            utils.waitAdvert(finalActivity);
            sleep(time)
          }
        } else {
          toastLog('观看广告任务已经结束')
          index = 3
          sleep(time)
        }
      }
      index++
    }
    back(); //做完任务返回
  }
}

function 宠物() {
  var 乐园 = text('乐园').findOne(30000)
  log("乐园:" + 乐园)
  if (乐园) {
    乐园.click()
    sleep(time)
    var 宠物乐园 = id('com.myth.pokemon:id/pets_park').findOne(1000)
    if (宠物乐园) {
      宠物乐园.click()
      utils.updateAppProgress(10);
      sleep(time)
      task(); //做任务
      utils.updateAppProgress(50);

  }
  var 喂食 = id('com.myth.pokemon:id/ivEatFood').findOne(1000)
  log("喂食:" + 喂食)
  if(喂食){
    喂食.click();
    var num = id("com.myth.pokemon:id/tvFoodNum").findOne(1000)
    id("com.myth.pokemon:id/etNum").setText(num.text()); //设置喂食数量
    log("本次喂食数量:" + num.text())
    var sure = text("确定").findOne(500);
    if(sure) sure.click();
   
    }
  }
}

function 种植物(){
  var 种植物 = text('种植物').findOne(5000)
  if (种植物) {
    var food = id('food').findOne(5000)
    if (food) {
      var food_num = parseFloat(food.text())
      toastLog('有' + food_num + '个种子')
      if (food_num > 0) {
        sleep(time)
        点击控件(种植物.parent())
        sleep(time)
        var i = 0
        var img_add = id('img_add').findOne(5000)
        if (img_add) {
          while (i < food_num) {
            img_add.click()
            sleep(500)
            i++
          }
        }
        var 确定 = text('确定').findOne(5000)
        if (确定) {
          sleep(time)
          确定.click()
        }
      } else {
        toastLog('没有种子')
        sleep(time)
      }
    }
  }
}
function main() {
  let open_app = launchAppEnhance("火爆乐园");
  if (open_app) {
   // task()
    宠物()
  }
  else {
    toastLog("火爆乐园打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
  var weight = x.bounds()
  click(weight.centerX(), weight.centerY())
}

storage.put("script_running_status", "end");